package com.woniu.custom.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.dto.UserDTO;
import com.woniu.common.utils.JWTUtil;
import com.woniu.common.utils.ResponseData;
import com.woniu.common.utils.ResponseEnum;
import com.woniu.custom.pojo.ZyUser;
import com.woniu.custom.service.ZyUserService;
import com.woniu.custom.mapper.ZyUserMapper;
import lombok.SneakyThrows;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
* @author xue
* @description 针对表【zy_user】的数据库操作Service实现
* @createDate 2024-08-07 11:18:02
*/
@Service
public class ZyUserServiceImpl extends ServiceImpl<ZyUserMapper, ZyUser>
    implements ZyUserService{
    @Autowired
    private ZyUserMapper userMapper;
    @Autowired
    private HttpServletResponse response;
    @Autowired
    private HttpServletRequest request;
    @Value("${salt}")
    private String salt;
    @SneakyThrows
    @Override
    public ResponseData login(ZyUser user) {
        //先判断账号是否存在
        ZyUser queryUser = userMapper.isExis(user);
        if (ObjectUtil.isEmpty(queryUser)) {
            return new ResponseData().fail(ResponseEnum.ACCOUNT_NOT_EXIST);
        }
        //判断密码是否正确
        String string = SecureUtil.md5(user.getUserPwd()+salt);
        if(!queryUser.getUserPwd().equals(string)){
            return new ResponseData().fail(ResponseEnum.LOGIN_ERROR);
        }
        //将登录信息存到jwt中
        Map<String, Object> map = new HashMap<>();
        map.put("id",queryUser.getId());
        map.put("userAccount",queryUser.getUserAccount());
        map.put("userCard",queryUser.getUserCard());
        map.put("userPhone",queryUser.getUserPhone());
        String jwt = JWTUtil.createJwt(map);
        Cookie cookie = new Cookie("jwt", jwt);
        cookie.setMaxAge(60*60*24*7);
        cookie.setPath("/");
        response.addCookie(cookie);
        return new ResponseData().success(queryUser);
    }


    @Override

    public ResponseData register(ZyUser user) {
        //先判断账号是否存在
        ZyUser queryUser = userMapper.isExis(user);
        if (!ObjectUtil.isEmpty(queryUser)) {
            return new ResponseData().fail(ResponseEnum.ACCOUNT_ALREADY_EXIST);
        }
        String string = SecureUtil.md5(user.getUserPwd() + salt);
        user.setUserPwd(string);
        save(user);
        return new ResponseData().success();
    }

    @SneakyThrows
    @Override
    public ResponseData isLogin() {
        Cookie[] cookies = request.getCookies();
        String jwt = null;
        for (Cookie cookie : cookies) {
            if(cookie.getName().equals("jwt")){
                jwt = cookie.getValue();
            }
        }
        if(StringUtils.isEmpty(jwt)){
            return new ResponseData().fail();
        }
        Map<String, Object> userInfo = JWTUtil.getUserInfo(jwt);
        String id = userInfo.get("id").toString();
        UserDTO userDTO = userMapper.queryUserById(id);
        return new ResponseData().success(userDTO);
    }
}




